home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / fpu881 / src6.zoo / pmlerr.c < prev    next >
Text File  |  1991-09-24  |  8KB  |  317 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *                N O T I C E                *
  4.  *                                    *
  5.  *            Copyright Abandoned, 1987, Fred Fish        *
  6.  *                                    *
  7.  *    This previously copyrighted work has been placed into the    *
  8.  *    public domain by the author (Fred Fish) and may be freely used    *
  9.  *    for any purpose, private or commercial.  I would appreciate    *
  10.  *    it, as a courtesy, if this notice is left in all copies and    *
  11.  *    derivative works.  Thank you, and enjoy...            *
  12.  *                                    *
  13.  *    The author makes no warranty of any kind with respect to this    *
  14.  *    product and explicitly disclaims any implied warranties of    *
  15.  *    merchantability or fitness for any particular purpose.        *
  16.  *                                    *
  17.  ************************************************************************
  18.  */
  19.  
  20.  
  21. /************************************************************************
  22.  *                                    *
  23.  *        PORTABLE MATH LIBRARY -- ERROR HANDLER            *
  24.  *                                    *
  25.  *    This is a sample PML library error handler.            *
  26.  *    It may be used as is, or another of the user's choice        *
  27.  *    substituted.                            *
  28.  *                                    *
  29.  *    In any case, the global "pmlerr" must be defined somewhere    *
  30.  *    in the user task, since many Portable Math Library routines    *
  31.  *    reference it.  The other routines in this file are not called    *
  32.  *    by any library routines and may be omitted.            *
  33.  *                                    *
  34.  ************************************************************************
  35.  */
  36.  
  37. # include <stdio.h>
  38. # include <pmluser.h>
  39. # include "pml.h"
  40.  
  41. static struct pml_err {
  42.     int flags;            /* Flag word; bits defined in pml.h    */
  43.     char *msg;            /* Error message            */
  44.     char *func;            /* Function in which error occured    */
  45. };
  46.  
  47. static struct pml_err pml_errs[] = {
  48.     CONTINUE | COUNT | LOG, "overflow", "exp",
  49.     CONTINUE | COUNT | LOG, "underflow", "exp",
  50.     CONTINUE | COUNT | LOG, "exponent overflow", "scale",
  51.     CONTINUE | COUNT | LOG, "negative argument", "sqrt",
  52.     CONTINUE | COUNT | LOG, "zero argument", "log",
  53.     CONTINUE | COUNT | LOG, "negative argument", "log",
  54.     CONTINUE | COUNT | LOG, "argument magnitude greater than 1.0", "acos",
  55.     CONTINUE | COUNT | LOG, "argument magnitude greater than 1.0", "asin",
  56.     CONTINUE | COUNT | LOG, "overflow", "tan",
  57.     CONTINUE | COUNT | LOG, "overflow", "cosh",
  58.     CONTINUE | COUNT | LOG, "underflow", "cosh",
  59.     CONTINUE | COUNT | LOG, "overflow", "sinh",
  60.     CONTINUE | COUNT | LOG, "underflow", "sinh",
  61.     CONTINUE | COUNT | LOG, "overflow", "asinh",
  62.     CONTINUE | COUNT | LOG, "argument less than 1.0", "acosh",
  63.     CONTINUE | COUNT | LOG, "overflow", "acosh",
  64.     CONTINUE | COUNT | LOG, "argument magnitude not  < 1.0", "atanh",
  65.     CONTINUE | COUNT | LOG, "underflow", "atan",
  66.     CONTINUE | COUNT | LOG, "complex division by zero", "cdiv",
  67.     CONTINUE | COUNT | LOG, "complex reciprocal of zero", "crcp",
  68.     CONTINUE | COUNT | LOG, "exponent underflow", "scale",
  69.     CONTINUE | COUNT | LOG, "argument has no fractional part", "dint",
  70. };
  71.  
  72. static int err_count = 0;        /* Counter for PML errors */
  73. static int err_limit = MAX_ERRORS;    /* PML error limit */
  74.  
  75. /*
  76.  *  FUNCTION
  77.  *
  78.  *    pmlcfs   Clear specified PML error handler flags
  79.  *
  80.  *  KEY WORDS
  81.  *
  82.  *    pmlcfs
  83.  *    machine independent routines
  84.  *    math libraries
  85.  *
  86.  *  DESCRIPTION
  87.  *
  88.  *    Clear the specified PML error handler flags for the
  89.  *    specified error.  Two or more flags may be cleared simultaneously
  90.  *    by "or-ing" them in the call, for example "LOG | CONTINUE".
  91.  *    The manifest constants for the flags and error codes are
  92.  *    defined in <pmluser.h>.
  93.  *
  94.  *  USAGE
  95.  *
  96.  *    pmlcfs(err_code,flags)
  97.  *    int err_code;
  98.  *    int flags;
  99.  *
  100.  *  PROGRAMMER
  101.  *
  102.  *    Fred Fish
  103.  *    Tempe, Az 85281
  104.  *    (602) 966-8871
  105.  *
  106.  */
  107.  
  108. pmlcfs(err_code,flag_word)
  109. register int err_code;
  110. register int flag_word;
  111. {
  112.     if (err_code < 0 || err_code > ((int)sizeof(pml_errs)/(int)sizeof(struct pml_err))) {
  113.     fprintf(stderr,"pmlcfs: invalid error code %d\n",err_code);
  114.     } else {
  115.     pml_errs[err_code].flags &= ~flag_word;
  116.     }
  117. }
  118.  
  119.  
  120. /*
  121.  *  FUNCTION
  122.  *
  123.  *    pmlcnt   get PML error count and reset it to zero
  124.  *
  125.  *  KEY WORDS
  126.  *
  127.  *    pmlcnt
  128.  *    machine independent routines
  129.  *    math libraries
  130.  *
  131.  *  DESCRIPTION
  132.  *
  133.  *    Returns the total number of PML errors seen
  134.  *    prior to the call, and resets the error count to zero.
  135.  *
  136.  *  USAGE
  137.  *
  138.  *    int pmlcnt()
  139.  *
  140.  *  PROGRAMMER
  141.  *
  142.  *    Fred Fish
  143.  *    Tempe, Az 85281
  144.  *    (602) 966-8871
  145.  *
  146.  */
  147.  
  148. int pmlcnt()
  149. {
  150.     register int rtn_val;
  151.  
  152.     rtn_val = err_count;
  153.     err_count = 0;
  154.     return(rtn_val);
  155. }
  156.  
  157.  
  158. /*
  159.  *  FUNCTION
  160.  *
  161.  *    pmlerr   Portable Math Library error handler
  162.  *
  163.  *  KEY WORDS
  164.  *
  165.  *    pmlerr
  166.  *    machine independent routines
  167.  *    math libraries
  168.  *
  169.  *  DESCRIPTION
  170.  *
  171.  *    Provides a sample PML error handler.  Does
  172.  *    not use any available hardware "traps" so is machine
  173.  *    independent.  Generally only called internally by the
  174.  *    other PML routines.
  175.  *
  176.  *    There are currently three flags which control the
  177.  *    response for specific errors:
  178.  *
  179.  *     (1)  LOG      When set an error message is sent
  180.  *                   to the user terminal.
  181.  *
  182.  *     (2)  COUNT    When set the error is counted
  183.  *                   against the PML error limit.
  184.  *
  185.  *     (3) CONTINUE  When set the task continues
  186.  *                   providing the error count has not
  187.  *                   exceeded the PML error limit.
  188.  *
  189.  *    Each of these flags can be set or reset independently
  190.  *    by "pmlsfs" or "pmlcfs" respectively.
  191.  *
  192.  *  USAGE
  193.  *
  194.  *    pmlerr(err_code)
  195.  *    int err_code;
  196.  *
  197.  *  PROGRAMMER
  198.  *
  199.  *    Fred Fish
  200.  *    Tempe, Az 85281
  201.  *    (602) 966-8871
  202.  *
  203.  */
  204.  
  205. pmlerr(err_code)
  206. register int err_code;
  207. {
  208.     register struct pml_err *err;
  209.  
  210.     if (err_code < 0 || err_code > ((int)sizeof(pml_errs)/(int)sizeof(struct pml_err))) {
  211.     fprintf(stderr,"pmlerr: invalid error code %d\n",err_code);
  212.     } else {
  213.     err = &pml_errs[err_code];
  214.     if (err->flags & LOG) {
  215.         fprintf(stderr,"pml: %s in function \"%s\"\n",err->msg,err->func);
  216.     }
  217.     if (err->flags & COUNT) {
  218.         err_count++;
  219.     }
  220.     if ((err->flags & CONTINUE) && (err_count <= err_limit)) {
  221.         return;
  222.     } else {
  223.         fprintf(stderr,"pml: error limit exceeded\n");
  224.         fprintf(stderr,"pml: task aborted with %d error(s)\n",
  225.         err_count);
  226.         exit(-1);
  227.     }
  228.     }
  229. }
  230.  
  231.  
  232. /*
  233.  *  FUNCTION
  234.  *
  235.  *    pmllim   Set Portable Math Library error limit 
  236.  *
  237.  *  KEY WORDS
  238.  *
  239.  *    pmllim
  240.  *    machine independent routines
  241.  *    math libraries
  242.  *
  243.  *  DESCRIPTION
  244.  *
  245.  *    Sets the PML error limit to the specified value
  246.  *    and returns it previous value.
  247.  *    Does not affect the current error count (which may be reset
  248.  *    to zero by a call to "pmlcnt").  Note that the default error
  249.  *    limit is set at compile time by the value in "pml.h".
  250.  *
  251.  *  USAGE
  252.  *
  253.  *    int pmllim(limit)
  254.  *    int limit;
  255.  *
  256.  *  PROGRAMMER
  257.  *
  258.  *    Fred Fish
  259.  *    Tempe, Az 85281
  260.  *    (602) 966-8871
  261.  *
  262.  */
  263.  
  264. int pmllim(limit)
  265. register int limit;
  266. {
  267.     register int rtn_val;
  268.  
  269.     rtn_val = err_limit;
  270.     err_limit = limit;
  271.     return(rtn_val);
  272. }
  273.  
  274.  
  275. /*
  276.  *  FUNCTION
  277.  *
  278.  *    pmlsfs   Set specified PML error handler flags
  279.  *
  280.  *  KEY WORDS
  281.  *
  282.  *    pmlsfs
  283.  *    machine independent routines
  284.  *    math libraries
  285.  *
  286.  *  DESCRIPTION
  287.  *
  288.  *    Set the specified PML error handler flags for the
  289.  *    specified error.  Two or more flags may be set simultaneously
  290.  *    by "or-ing" them in the call, for example "LOG | CONTINUE".
  291.  *    The manifest constants for the flags and error codes are
  292.  *    defined in <pmluser.h>.
  293.  *
  294.  *  USAGE
  295.  *
  296.  *    pmlsfs(err_code,flags)
  297.  *    int err_code;
  298.  *    int flags;
  299.  *
  300.  *  PROGRAMMER
  301.  *
  302.  *    Fred Fish
  303.  *    Tempe, Az 85281
  304.  *
  305.  */
  306.  
  307. pmlsfs(err_code,flag_word)
  308. register int err_code;
  309. register int flag_word;
  310. {
  311.     if (err_code < 0 || err_code > ((int)sizeof(pml_errs)/(int)sizeof(struct pml_err))) {
  312.     fprintf(stderr,"? pmlsfs --- invalid error code %d.\n",err_code);
  313.     } else {
  314.     pml_errs[err_code].flags |= flag_word;
  315.     }
  316. }
  317.